package com.boot.stomp;


import com.boot.stomp.authentication.StompAuthenticationHandler;
import com.boot.stomp.client.AccountClient;
import com.boot.stomp.interceptor.MultipleChannelInterceptor;
import com.boot.stomp.service.StompConsumerService;
import com.boot.stomp.service.impl.DefaultStompConsumerServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.support.ChannelInterceptor;

@Configuration
public class SecurityStompAutoConfiguration {

    @Bean
    StompAuthenticationHandler authenticationHandler(AccountClient accountClient) {
        return new TokenAuthenticationHandler(accountClient);
    }

    @Bean
    ChannelInterceptor channelInterceptor(StompAuthenticationHandler[] handlers) {
        return new MultipleChannelInterceptor(handlers);
    }

    @ConditionalOnMissingBean(value = String.class, parameterizedContainer = StompConsumerService.class, name = "stomp_consumer_service")
    @Bean("stomp_consumer_service")
    StompConsumerService<String> defaultStompConsumerServiceImpl(SimpMessagingTemplate template) {
        return new DefaultStompConsumerServiceImpl<>(template);
    }

}
